package com.raja.requestForwarding.mq.factory;

import com.rabbitmq.client.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * Created by Administrator on 2017/8/16/016.
 */
@Component
public class RabbitMqFactory {
    private static final Logger log = LoggerFactory.getLogger(RabbitMqFactory.class);

    public ConnectionFactory factory =  null;
    @Value("${spring.rabbitmq.host}")
    private String host;
    @Value("${spring.rabbitmq.port}")
    private String port;
    @Value("${spring.rabbitmq.username}")
    private String username;
    @Value("${spring.rabbitmq.password}")
    private String password;

    public synchronized   ConnectionFactory init() throws Exception{
        if(factory == null) {
            factory = new ConnectionFactory();
            factory.setHost(host);
            factory.setPort(Integer.valueOf(port));
            factory.setUsername(username);
            factory.setPassword(password);
        }
        return factory;
    }

}
